﻿#region Copyright and License Notice
// Copyright (C)2010-2016 - Rob Levine and other contributors
// http://configgen.codeplex.com
// 
// This file is part of ConfigGen.
// 
// ConfigGen is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// 
// ConfigGen is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
// 
// You should have received a copy of the GNU General Public License and 
// the GNU Lesser General Public License along with ConfigGen.  
// If not, see <http://www.gnu.org/licenses/>
#endregion

using System;
using System.Collections.Generic;
using System.Text;

namespace ConfigGen.Utilities.Extensions.System
{
    /// <summary>
    /// Extensions for <see cref="string"/> instances
    /// </summary>
    public static class StringExtensions
    {
        /// <summary>
        /// Returns an enumerable list of lines of text which are the result of word-wrapping the source string to the max length specified.
        /// </summary>
        /// <remarks>This word wrap is crude, and merely wraps on spaces between words.</remarks>
        /// <param name="source">Source string</param>
        /// <param name="maxLineLength">Max length</param>
        /// <returns>Lines of wrapped text</returns>
        /// <exception cref="ArgumentNullException">Thrown if <paramref name="source"/> is null.</exception>
        /// <exception cref="ArgumentOutOfRangeException">Thrown if <paramref name="maxLineLength"/> is less than one.</exception>
        public static IEnumerable<string> WordWrap(this string source, int maxLineLength)
        {
            if (source == null) throw new ArgumentNullException("source");
            if (maxLineLength < 1) throw new ArgumentOutOfRangeException("maxLineLength");
            
            if (source.Length <= maxLineLength)
            {
                yield return source;
            }
            else
            {
                var words = new Queue<string>(source.Split(' '));
                var sb = new StringBuilder();

                while (words.Count > 0)
                {
                    // always put at least one word on a line
                    if (sb.Length == 0)
                    {
                        sb.Append(words.Dequeue());
                    }
                    else if (sb.Length + words.Peek().Length + 1 <= maxLineLength)
                    {
                        sb.Append(" ");
                        sb.Append(words.Dequeue());
                    }
                    else
                    {
                        yield return sb.ToString();
                        sb = new StringBuilder();
                    }
                }

                if (sb.Length > 0)
                {
                    yield return sb.ToString();
                }
            }
        }

        /// <summary>
        /// Returns true if the supplied string is null or zero length, otherwise false.
        /// </summary>
        /// <param name="str">The string to test</param>
        /// <returns>True if the supplied string is null or zero length, otherwise false</returns>
        public static bool IsNullOrEmpty(this string str)
        {
            return string.IsNullOrEmpty(str);
        }
    }
}
